package com.lc.luzhengtang.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.adapter.ProfitListAdapter3;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.ShouquanBean;
import com.lc.luzhengtang.conn.ConnTamList;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/6/20.
 */

public class TeamActivityListActivity2 extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.text_normal_person)
    TextView textNormalPerson;
    @BindView(R.id.recyclerView_list)
    RecyclerView recyclerViewList;

    List<ShouquanBean.DataBeanX.DataBean> recycle_list;
    ProfitListAdapter3 profitListAdapter;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    String flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team2);
        ButterKnife.bind(this);
        recycle_list = new ArrayList<>();
        recyclerViewList.setLayoutManager(new LinearLayoutManager(this));
    /*    for (int i = 0; i < 10; i++) {
            recycle_list.add("呵呵" + i);
        }*/
//        textNormalPerson


        initData();
//        getData();
    }


    protected void initData() {
        recyclerViewList.setItemAnimator(new DefaultItemAnimator());
        //recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerViewList.setLayoutManager(new LinearLayoutManager(this));

        profitListAdapter = new ProfitListAdapter3(null);
        profitListAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        profitListAdapter.isFirstOnly(false);
//        Drawable drawable = ContextCompat.getDrawable(TeamActivityCommonListActivity.this, R.drawable.line_divider);
//        recyclerViewList.addItemDecoration(new SimpleDividerItemDecoration(TeamActivityCommonListActivity.this, drawable, 1));

        recyclerViewList.setAdapter(profitListAdapter);

        refreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
        refreshLayout.setOnRefreshListener(this);
        profitListAdapter.setOnLoadMoreListener(this);

        //开启loading,获取数据
        setRefreshing(true);
        onRefresh();
    }

    public void setRefreshing(final boolean refreshing) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(refreshing);
            }
        });
    }

    @OnClick({R.id.img_news, R.id.text_normal_person, R.id.recyclerView_list})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                finish();
                break;
            case R.id.text_normal_person:
                break;
            case R.id.recyclerView_list:
                break;
        }
    }

    @Override
    public void onRefresh() {
        new ConnTamList(BaseApplication.basePreferences.readUid(), getIntent().getStringExtra("role_id"), "1", new AsyCallBack<ShouquanBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                profitListAdapter.setEnableLoadMore(false);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, ShouquanBean s) throws Exception {
                super.onSuccess(toast, type, o, s);

                recycle_list = s.getData().getData();

                if (recycle_list != null) {
//                    currentPage = 1;
                    profitListAdapter.setNewData(recycle_list);
                }
                flag = getIntent().getStringExtra("myflag");
                String string="("+  recycle_list.size()+""+")";

                if (flag.equals("2")) {
                    textNormalPerson.setText(getIntent().getStringExtra("textRole_submit2")+string);
                } else if (flag.equals("3")) {
                    textNormalPerson.setText(getIntent().getStringExtra("textRole_submit3")+string);
                } else if (flag.equals("4")){
                    textNormalPerson.setText(getIntent().getStringExtra("textRole_submit4")+string);
                }else {
                    textNormalPerson.setText("普通会员"+string);
                }

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                setRefreshing(false);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                profitListAdapter.removeAllFooterView();
                //最后调用结束刷新的方法
                setRefreshing(false);

                profitListAdapter.setEnableLoadMore(false);
            }
        }


        ).execute(TeamActivityListActivity2.this, false);
    }

    @Override
    public void onLoadMoreRequested() {
        new ConnTamList(BaseApplication.basePreferences.readUid(), getIntent().getStringExtra("role_id"), "1", new AsyCallBack<ShouquanBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                showLoading();
                profitListAdapter.setEnableLoadMore(true);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, ShouquanBean s) throws Exception {
                super.onSuccess(toast, type, o, s);
                /*recycle_list = s.getData().getData();

                profitListAdapter = new ProfitListAdapter2(recycle_list);
                recyclerViewList.setAdapter(profitListAdapter);
*/

              /*  recycle_list = s.getData().getData();

                if (recycle_list != null) {
                    currentPage = 1;
                    profitListAdapter.setNewData(recycle_list);
                }
*/

                recycle_list = s.getData().getData();

                if (recycle_list != null && recycle_list.size() > 0) {
//                    currentPage++;
                    profitListAdapter.setNewData(recycle_list);
                } else {
                    //显示没有更多数据
                    // newsAdapter.loadComplete();
                    profitListAdapter.loadMoreComplete();
                    View noDataView = LayoutInflater.from(TeamActivityListActivity2.this).inflate(R.layout.item_no_data, (ViewGroup) recyclerViewList.getParent(), false);
                    profitListAdapter.addFooterView(noDataView);
                }


            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                refreshLayout.setRefreshing(false);
                dismissLoading();
                profitListAdapter.removeAllFooterView();
                //最后调用结束刷新的方法
                profitListAdapter.setEnableLoadMore(false);
                setRefreshing(false);
            }
        }


        ).execute(TeamActivityListActivity2.this, false);
    }
}
